<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="租户id" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="物资名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级分类" prop="classify">
        <el-select v-model="queryParams.classify" placeholder="请选择一级分类" clearable>
          <el-option
            v-for="dict in dict.type.materiel_classify"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="二级分类" prop="classifyS">
        <el-input
          v-model="queryParams.classifyS"
          placeholder="请输入二级分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="适合宠物类型" prop="petType">
        <el-select v-model="queryParams.petType" placeholder="请选择适合宠物类型" clearable>
          <el-option
            v-for="dict in dict.type.pet_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保质期
" prop="expirationDate">
        <el-input
          v-model="queryParams.expirationDate"
          placeholder="请输入保质期
"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="条形码" prop="barCode">
        <el-input
          v-model="queryParams.barCode"
          placeholder="请输入条形码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更多条形码" prop="barCodeS">
        <el-input
          v-model="queryParams.barCodeS"
          placeholder="请输入更多条形码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标准进货价" prop="standardPurchasingCost">
        <el-input
          v-model="queryParams.standardPurchasingCost"
          placeholder="请输入标准进货价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平均成本价格" prop="averageCostPrice">
        <el-input
          v-model="queryParams.averageCostPrice"
          placeholder="请输入平均成本价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零售价" prop="retailPrice">
        <el-input
          v-model="queryParams.retailPrice"
          placeholder="请输入零售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员价" prop="memberPrice">
        <el-input
          v-model="queryParams.memberPrice"
          placeholder="请输入会员价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否支持负库存销售 0是1否" prop="negativeOnHandType">
        <el-select v-model="queryParams.negativeOnHandType" placeholder="请选择是否支持负库存销售 0是1否" clearable>
          <el-option
            v-for="dict in dict.type.negative_on_hand_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库存预警数" prop="inventoryWarning">
        <el-input
          v-model="queryParams.inventoryWarning"
          placeholder="请输入库存预警数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否参与会员打折" prop="memberDiscountType">
        <el-select v-model="queryParams.memberDiscountType" placeholder="请选择是否参与会员打折" clearable>
          <el-option
            v-for="dict in dict.type.member_discount_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否复购提醒" prop="repurchaseType">
        <el-select v-model="queryParams.repurchaseType" placeholder="请选择是否复购提醒" clearable>
          <el-option
            v-for="dict in dict.type.repurchase_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物资id" prop="materielId">
        <el-input
          v-model="queryParams.materielId"
          placeholder="请输入物资id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hospital:Materiel:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hospital:Materiel:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hospital:Materiel:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hospital:Materiel:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="MaterielList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
<!--      <el-table-column label="租户id" align="center" prop="tenantId" />
      <el-table-column label="部门id" align="center" prop="deptId" />-->
      <el-table-column label="物资名称" align="center" prop="name" />
      <el-table-column label="一级分类" align="center" prop="classify">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.materiel_classify" :value="scope.row.classify"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="二级分类" align="center" prop="classifyS" />-->
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="适合宠物类型" align="center" prop="petType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pet_type" :value="scope.row.petType"/>
        </template>
      </el-table-column>
      <el-table-column label="保质期
" align="center" prop="expirationDate" />
      <el-table-column label="条形码" align="center" prop="barCode" />
      <el-table-column label="更多条形码" align="center" prop="barCodeS" />
      <el-table-column label="规格型号" align="center" prop="specification" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="标准进货价" align="center" prop="standardPurchasingCost" />
      <el-table-column label="平均成本价格" align="center" prop="averageCostPrice" />
      <el-table-column label="零售价" align="center" prop="retailPrice" />
      <el-table-column label="会员价" align="center" prop="memberPrice" />
      <el-table-column label="是否支持负库存销售 0是1否" align="center" prop="negativeOnHandType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.negative_on_hand_type" :value="scope.row.negativeOnHandType"/>
        </template>
      </el-table-column>
      <el-table-column label="库存预警数" align="center" prop="inventoryWarning" />
      <el-table-column label="是否参与会员打折" align="center" prop="memberDiscountType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.member_discount_type" :value="scope.row.memberDiscountType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否复购提醒" align="center" prop="repurchaseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repurchase_type" :value="scope.row.repurchaseType"/>
        </template>
      </el-table-column>
      <el-table-column label="重量" align="center" prop="weight" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="物资id" align="center" prop="materielId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hospital:Materiel:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hospital:Materiel:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物资对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="租户id" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户id" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>-->

        <el-row>
          <el-col :span="12">
            <el-form-item label="物资名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入物资名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="宠物类型" prop="petType">
              <el-select v-model="form.petType" placeholder="请选择适合宠物类型">
                <el-option
                  v-for="dict in dict.type.pet_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="分类" prop="classify">
              <el-select v-model="form.classify" placeholder="请选择分类">
                <el-option
                  v-for="dict in dict.type.materiel_classify"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
<!--            <el-form-item label="二级分类" prop="classifyS">
              <el-input v-model="form.classifyS" placeholder="请输入二级分类" />
            </el-form-item>-->
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商" prop="supplier">
              <el-input v-model="form.supplier" placeholder="请输入供应商" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌" prop="brand">
              <el-input v-model="form.brand" placeholder="请输入品牌" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="条形码" prop="barCode">
              <el-input v-model="form.barCode" placeholder="请输入条形码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="条形码2" prop="barCodeS">
              <el-input v-model="form.barCodeS" placeholder="请输入更多条形码" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="保质期" prop="expirationDate">
              <el-input v-model="form.expirationDate" placeholder="请输入保质期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重量" prop="weight">
              <el-input v-model="form.weight" placeholder="请输入重量" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="零售价" prop="retailPrice">
              <el-input v-model="form.retailPrice" placeholder="请输入零售价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员价" prop="memberPrice">
              <el-input v-model="form.memberPrice" placeholder="请输入会员价" />
            </el-form-item>
          </el-col>
        </el-row>




        <div> <h1>高级设置</h1></div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="支持负库存销售" prop="negativeOnHandType">
              <el-select v-model="form.negativeOnHandType" placeholder="请选择是否支持负库存销售">
                <el-option
                  v-for="dict in dict.type.negative_on_hand_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参与会员打折" prop="memberDiscountType">
              <el-select v-model="form.memberDiscountType" placeholder="请选择是否参与会员打折">
                <el-option
                  v-for="dict in dict.type.member_discount_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="库存预警数" prop="inventoryWarning">
              <el-input v-model="form.inventoryWarning" placeholder="请输入库存预警数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="复购提醒" prop="repurchaseType">
              <el-select v-model="form.repurchaseType" placeholder="请选择是否复购提醒">
                <el-option
                  v-for="dict in dict.type.repurchase_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">

          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="物资id" prop="materielId">
          <el-input v-model="form.materielId" placeholder="请输入物资id" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMateriel, getMateriel, delMateriel, addMateriel, updateMateriel } from "@/api/hospital/Materiel";

export default {
  name: "Materiel",
  dicts: ['negative_on_hand_type', 'member_discount_type', 'pet_type', 'repurchase_type', 'materiel_classify'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物资表格数据
      MaterielList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        deptId: null,
        name: null,
        classify: null,
        classifyS: null,
        supplier: null,
        brand: null,
        petType: null,
        expirationDate: null,
        barCode: null,
        barCodeS: null,
        specification: null,
        unit: null,
        standardPurchasingCost: null,
        averageCostPrice: null,
        retailPrice: null,
        memberPrice: null,
        negativeOnHandType: null,
        inventoryWarning: null,
        memberDiscountType: null,
        repurchaseType: null,
        weight: null,
        materielId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tenantId: [
          { required: true, message: "租户id不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "物资名称不能为空", trigger: "blur" }
        ],
        retailPrice: [
          { required: true, message: "零售价不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物资列表 */
    getList() {
      this.loading = true;
      listMateriel(this.queryParams).then(response => {
        this.MaterielList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        deptId: null,
        name: null,
        classify: null,
        classifyS: null,
        supplier: null,
        brand: null,
        petType: null,
        expirationDate: null,
        barCode: null,
        barCodeS: null,
        specification: null,
        unit: null,
        standardPurchasingCost: null,
        averageCostPrice: null,
        retailPrice: null,
        memberPrice: null,
        negativeOnHandType: null,
        inventoryWarning: null,
        memberDiscountType: null,
        repurchaseType: null,
        weight: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        materielId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物资";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMateriel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物资";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMateriel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMateriel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除物资编号为"' + ids + '"的数据项？').then(function() {
        return delMateriel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hospital/Materiel/export', {
        ...this.queryParams
      }, `Materiel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
